using System;

namespace Flaschengeist.Processes
{
	/// <summary>
	/// Calculate the available fluid by reference the level track 
	/// for filling glasses in ml.
	/// </summary>
	public class CalculateTrackFluid
	{
		private float maxFillLevel;
	    private string[] scale;
		
		public CalculateTrackFluid(float maxFillLevel, string[] scale)
		{		       
			this.maxFillLevel = maxFillLevel;
	        this.scale = scale;		
		}
		
		public void Process(string[] levelTrack)
		{
			int interval = (int)(maxFillLevel/scale.Length);
			int totalFluid = 0;
			
			for(int i = 0; i < levelTrack.Length; i++)
			{
				totalFluid += interval * GetScalePosition(levelTrack[i]);
				//Pro Glas interval Einheiten extra (Puffer)
				totalFluid += interval;
			}
			
			Completed(totalFluid);
		}
		
		public event Action<int> Completed;
		
		private int GetScalePosition(string note)
		{
			for (int i = 1; i < scale.Length+1; i++) 	
			{
				if(note == scale[i-1])
					return i;
			}
			
			return 0;
		}
	}
}

